/*
package org.itboys.mobile.service.mysql.mobile.product;

import org.itboys.admin.dto.lasted.SundayUploadDto;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.kitchen.entity.mongo.village.KitchenVillage;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayCommentDto;
import org.itboys.mobile.dto.product.SundayProductDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.SundayPermission;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mongo.mobile.KitchenMobileVillageService;
import org.itboys.mobile.service.mongo.mobile.SundayMobileCommentService;
import org.itboys.mobile.service.mongo.mobile.SundayMobilePermissionService;
import org.itboys.mobile.service.mongo.mobile.collection.SundayMobileCollectionService;
import org.itboys.mobile.service.mongo.mobile.product.KitchenMobileProductCookBookService;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mongodb.service.BaseMySqlService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

*/
/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_service_mobile
 *//*

@Service
public class SundayMobileProductService_ld extends BaseMySqlService<SundayProduct> {
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private SundayMobileCollectionService collectionService;
    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private SundayMobileCommentService commentService;
    @Autowired
    private KitchenMobileProductCookBookService productCookBookService;
    @Autowired
    private SundayMobilePermissionService permissionService;
    @Autowired
    private KitchenMobileVillageService villageService;
    @Autowired
    private SystemDictionaryService dictionaryService;
    */
/**
     * 分页获取商品
     * @param request
     * @param memberId
     * @param type
     *//*

    public List<SundayProductDto> selectProduct(HttpServletRequest request,Long memberId,Long villageId,Integer type){
            Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"name","categoryId");
            param.put("status",1);
            //1商城商品，2积分商品，3采购商品
            if(type!=1&&type!=2&&type!=3){
                param.put("type",0);
            }else{
                //采购商品也是商城商品
                if(type==3)type=1;
                param.put("type",type);
            }

            return packageDtoMuilt(super.select(param),memberId,villageId);
    }

    */
/**
     * 查询商品详情
     * @param memberId
     * @param productId
     * @param villageId
     *//*

    public SundayProductDto findOne(Long memberId,Long productId,Long villageId){
        return packageDtoSingle(super.find(productId),memberId,villageId);

    }
    */
/**
     * 查询商品详情(分享时使用)

     * @param productId

     *//*

    public SundayProduct share(Long productId){
        SundayProduct product =super.find(productId);
        //加载轮播图
        List<SundayUploadDto> rollImages = uploadService.selectNoPage(productId,SundayProduct.class.getSimpleName());
        product.setRollImages(rollImages);
        return product;

    }
    */
/** 组装DTO
     * 不同的用户类型。商品价格享受价格不同
     * 1，普通用户。零售价格
     * 2，门店。批发价格
     * 3，大客户。折扣价格
     *
     *//*

    public SundayProductDto packageDtoSingle(SundayProduct product, Long memberId,Long villageId){

        SundayProductDto dto = null;
        //商城商品根据用户区别价格价格
        //getRealPrice(product,null,memberId);
        //product= getRealPrice2(Arrays.asList(product),memberId,null,villageId).get(0);
        //组装轮播图
        List<SundayUploadDto> rollImages = uploadService.selectNoPage(product.getId(),SundayProduct.class.getSimpleName());
        //商品详情URL
        String descUrl =resourceHolder.getStringValue("mobileRoot")+"/sunday/mobile/product/desc?id="+product.getId();
        //判断受否收藏
        Integer isCollection =collectionService.checkIsCollection(memberId,SundayCommonConstants.collection_type_product,product.getId());
        //加载默认评论
        logger.info("加载评论");
        List<SundayCommentDto> comments = commentService.getCommentByNumber(2,product.getId(),SundayProduct.class.getSimpleName());
        logger.info("加载评论-结束");
        //List<SundayCommentDto> comments =null;
        //加载推荐商品
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("categoryId",product.getCategoryId());
        param.put("status",1);
        param.put("row_start",0);
        param.put("row_size",2);
        param.put("type",product.getType());
       // List<SundayProductDto> productDtos  =packageDtoMuilt(super.select(param),memberId);
        List<SundayProductDto> productDtos  =packageDtoMuilt(super.select(param),memberId,villageId);
        List<Map<String,Object>> reProducts = new ArrayList<Map<String,Object>>();
        for(SundayProductDto productDto:productDtos){
            Map<String,Object> reProduct = new HashMap<String,Object>();
            reProduct.put("id",productDto.getId());
            reProduct.put("name",productDto.getName());
            reProduct.put("viceName",productDto.getViceName());
            reProduct.put("image",productDto.getImage());
            reProduct.put("price",productDto.getPrice());
            reProduct.put("marketPrice",productDto.getMarketPrice());
            reProducts.add(reProduct);
        }
        dto=  new SundayProductDto(product.getId(), product.getCreateTime(), product.getCreateDate(),
                product.getName()+product.getStandard(),  product.getViceName(),   product.getStandard(),  product.getMarketPrice(), product.getPrice(),  product.getType(),
                resourceHolder.getStringValue("imgRoot")+product.getImage(),  product.getSaleNumber(), rollImages,  product.getCostPrice(),
                isCollection,  descUrl,  reProducts, comments,product.getStore());
        //分享地址
        dto.setShareUrl(resourceHolder.getStringValue("mobileRoot")+"/sunday/mobile/product/share?id="+product.getId());
        dto.setShareImage(product.getImage());
        dto.setShareTitle("花果山果蔬 产品分享");
        dto.setShareDesc(product.getName()+product.getViceName()+product.getStandard());

        //加载关联菜谱
        List<Map<String,Object>> cookBooks = productCookBookService.getCookBooksByProduct(product.getId());
        dto.setCookBooks(cookBooks);

        return dto;
    }
    public List<SundayProductDto> packageDtoMuilt(List<SundayProduct> products,Long memberId,Long villageId){
        List<SundayProductDto> dtos = new ArrayList<SundayProductDto>();
        //获取商品真实价格
       // getRealPrice(products,memberId);
      //  products=getRealPrice2(products,memberId,null,villageId);
        for(SundayProduct product:products){
            //主图
            dtos.add(new SundayProductDto(product.getId(), product.getCreateTime(), product.getCreateDate(),
                    product.getName()+product.getStandard(),  product.getViceName(),   product.getStandard(),  product.getMarketPrice(), product.getPrice(),  product.getType(),
                    resourceHolder.getStringValue("imgRoot")+product.getImage(),  product.getSaleNumber(),  product.getCostPrice(),product.getStore()));

        }
       return dtos;
    }


    */
/**
     * 根据用户类型获取商品真实价格和真实类型
     * @param product
     * @param member
     *//*

 */
/*  public void getRealPrice(SundayProduct product,SundayMember member,Long memberId){
        if(member==null){
            member=memberService.getById(memberId);
        }
        BigDecimal price = product.getPrice();
        String image=resourceHolder.getStringValue("imgRoot")+product.getImage();

        //设置过渡字段，商品零售价


        if(product.getType()==SundayCommonConstants.type_mall){
            //用户价格
            if(member.getUserType()== SundayCommonConstants.user_type_member){
                //门店价格
            }else if(member.getUserType()==SundayCommonConstants.user_type_shop){
                price =product.getWholesalePrice();
                product.setType(SundayCommonConstants.type_caigou);
              //大客户价格
            }else if(member.getUserType()==SundayCommonConstants.user_type_customer){
                BigDecimal discount =new BigDecimal(member.getDiscount().doubleValue()/100).setScale(2,BigDecimal.ROUND_HALF_UP);
                price=price.multiply(discount).setScale(2,BigDecimal.ROUND_HALF_UP);
                product.setType(SundayCommonConstants.type_caigou);
            }

        }
        product.setRetailPrice(product.getPrice());
        product.setPrice(price);
        product.setImage(image);
    }
*//*

 */
/**
     * 根据用户类型获取商品真实价格
     * @param products
     * @param memberId
     *//*
*/
/*
    public void getRealPrice(List<SundayProduct> products,Long memberId){
               SundayMember member=memberService.getById(memberId);
            for(SundayProduct product:products){
                getRealPrice(product,member,null);
            }
    }*//*

   // public void

    */
/**
     * 根据用户类型，和用户当前小区，获取商品价格
     * @param products
     * @param memberId
     * @param member
     *//*

   */
/* public List<SundayProduct> getRealPrice2(List<SundayProduct> products,Long memberId,SundayMember member,Long villageId) {
        if (member == null) {
            member = memberService.getById(memberId);
        }

        KitchenVillage village = null;
        boolean isStartShopStoreAndPrice = false;
        List<SystemDictionary> dictionaries = dictionaryService.getDictionaryByKey("isStartShopStoreAndPrice");
        List<SundayPermission> permissions = new ArrayList<SundayPermission>();
        //是否开启，门店价格和库存认证
        if (ListUtil.isNotNull(dictionaries) &&
                dictionaries.get(0).getValue().equalsIgnoreCase("1") &&
                LongUtil.isNotZreo(villageId) &&
                member.getUserType() == SundayCommonConstants.user_type_member) {
            isStartShopStoreAndPrice = true;

                village = villageService.getById(villageId);
                //门店的库存和价格记录
                Map<String, Object> param = new HashMap<String, Object>();
                param.put("memberId", village.getShopId());
                param.put("type", 1);
                param.put("isDeleted", 0);
                if (ListUtil.isNotNull(products)) {
                    List<Long> productIds = new ArrayList<Long>();
                    for (SundayProduct p : products) {
                        productIds.add(p.getId());
                    }
                    param.put("otherId in", productIds);
                }
                permissions = permissionService.list(param);
                for (SundayPermission permission : permissions) {
                    logger.info("当前门店+存储的信息==" + permission.getOtherId() + "-store=" + permission.getStore() + "-price=" + permission.getPrice());

                }

        }


        for (int i = 0; i < products.size(); i++) {
            SundayProduct product = products.get(i);
            BigDecimal price = product.getPrice();
            String image = resourceHolder.getStringValue("imgRoot") + product.getImage();
            product.setStore(0);
            logger.info("当前商品信息，循环下班="+i+"=商品ID="+product.getId()+"-store="+product.getStore()+"-price="+product.getPrice());

            if (product.getType() == SundayCommonConstants.type_mall) {
                //用户价格,按当前所在小区获取价格
                if (member.getUserType() == SundayCommonConstants.user_type_member) {
                    if (isStartShopStoreAndPrice) {
                        //获取门店自定义的价格和库存
                        //如果没有设置库存，执行0库存和标准商品价格

                        //  boolean isZeroStore=true;
                        for (SundayPermission permission : permissions) {
                            if (LongUtil.isNotZreo(permission.getOtherId()) &&
                                    permission.getOtherId().longValue() == product.getId().longValue()) {
                               // logger.info("productId==" + product.getId() + "-store=" + permission.getStore() + "-price=" + permission.getPrice());
                                if (permission.getStore() != null && permission.getStore() > 0) {
                                    //isZeroStore=false;
                                    product.setStore(permission.getStore());
                                    if (permission.getPrice() != null) {
                                        product.setPrice(new BigDecimal(permission.getPrice()));
                                    }
                                    break;
                                }

                            }
                        }
                        //logger.info("商品校验后信息==" + product.getId() + "-store=" + product.getStore() + "-price=" + product.getPrice());
                    }


                    //门店价格
                } else if (member.getUserType() == SundayCommonConstants.user_type_shop) {
                    //price =product.getWholesalePrice();
                    product.setPrice(product.getWholesalePrice());
                    product.setType(SundayCommonConstants.type_caigou);
                    //大客户价格
                } else if (member.getUserType() == SundayCommonConstants.user_type_customer) {
                    BigDecimal discount = new BigDecimal(member.getDiscount().doubleValue() / 100).setScale(2, BigDecimal.ROUND_HALF_UP);
                    ;
                    product.setPrice(price.multiply(discount).setScale(2, BigDecimal.ROUND_HALF_UP));
                    product.setType(SundayCommonConstants.type_caigou);
                }
                product.setRetailPrice(product.getPrice());
                //product.setPrice(price);
                product.setImage(image);
            }

        }
       // logger.info("删除前商品集合大小="+products.size());
        //删除库存为0的商品。
        //返回新的集合
        List<SundayProduct> results =new ArrayList<SundayProduct>();
        for (int i = 0; i < products.size(); i++){
            SundayProduct product = products.get(i);
           // logger.info("商品类型="+product.getType()+"--用户类型-="+member.getUserType()+"-库存="+product.getStore());

            if (product.getType() == SundayCommonConstants.type_mall&&
                    member.getUserType() == SundayCommonConstants.user_type_member&&
                    isStartShopStoreAndPrice
                    ) {
                if(product.getStore()>0){
                    results.add(product);
                }

            }else{
                results.add(product);
            }
        }
        logger.info("删除后商品集合大小="+results.size());
        return results;
    }*//*


    */
/**
     * 按照小区，获取商品的真实库存和价格。此价格为不打折价格
     * @param products
     * @param memberId
     * @param member
     * @param villageId
     * @param isCheckStore 是否校验库存。将库存不满足要求商品，不与显示
     * @return
     *//*

    public List<SundayProduct> getRealPrice3(List<SundayProduct> products,Long memberId,SundayMember member,Long villageId,boolean isCheckStore) {
        Map<String, Object> param = new HashMap<String, Object>();
        //最后返回的商品集合
        List<SundayProduct> returnProducts=new ArrayList<SundayProduct>();
        if (member == null) {
            member = memberService.getById(memberId);
        }



        //app的最小上架库存量
        Integer  minimumStore= Integer.valueOf(dictionaryService.getDictionaryByKey2("minimumStore").getValue());
        List<SundayPermission> permissions = new ArrayList<SundayPermission>();
        //获取当前商品真实价格和库存。只对【用户版本生效】
        if(member.getUserType()==SundayCommonConstants.user_type_member){
            KitchenVillage village = villageService.getById(villageId);
            List<Long> productIds = new ArrayList<Long>();
            for (SundayProduct p : products) {
                productIds.add(p.getId());
            }
            param.clear();
            param.put("memberId", village.getShopId());
            param.put("productId in", productIds);
            param.put("isDeleted", 0);
            permissions=permissionService.list(param);
        }
        //设置商品的库存和价格
        for(SundayProduct product:products){
            //只有用户购物才读取
            if(member.getUserType()==SundayCommonConstants.user_type_member){
                for(SundayPermission permission:permissions){
                    if(LongUtil.isNotZreo(permission.getProductId())&&
                            product.getId().longValue()==permission.getProductId().longValue()){
                        product.setPrice(new BigDecimal(permission.getPrice()));
                        product.setStore(permission.getStore());

                        //返回符合最低库存要求的商品
                        if(isCheckStore&&product.getStore()>=minimumStore){
                             products.add(product);
                        }
                        break;
                    }
                }
            }else{
                returnProducts.add(product);
            }
        }
        return returnProducts;
    }
}
*/
